package org.dromara.manu.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.manu.domain.vo.ManuKpiMasterVo;
import org.dromara.manu.domain.bo.ManuKpiMasterBo;
import org.dromara.manu.service.IManuKpiMasterService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 主数据
 *
 * @author Bin Liu
 * @date 2024-08-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manu/kpiMaster")
public class ManuKpiMasterController extends BaseController {

    private final IManuKpiMasterService manuKpiMasterService;

    /**
     * 查询主数据列表
     */
    @SaCheckPermission("manu:kpiMaster:list")
    @GetMapping("/list")
    public TableDataInfo<ManuKpiMasterVo> list(ManuKpiMasterBo bo, PageQuery pageQuery) {
        return manuKpiMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出主数据列表
     */
    @SaCheckPermission("manu:kpiMaster:export")
    @Log(title = "主数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ManuKpiMasterBo bo, HttpServletResponse response) {
        List<ManuKpiMasterVo> list = manuKpiMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "主数据", ManuKpiMasterVo.class, response);
    }

    /**
     * 获取主数据详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("manu:kpiMaster:query")
    @GetMapping("/{id}")
    public R<ManuKpiMasterVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(manuKpiMasterService.queryById(id));
    }

    /**
     * 新增主数据
     */
    @SaCheckPermission("manu:kpiMaster:add")
    @Log(title = "主数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ManuKpiMasterBo bo) {
        return toAjax(manuKpiMasterService.insertByBo(bo));
    }

    /**
     * 修改主数据
     */
    @SaCheckPermission("manu:kpiMaster:edit")
    @Log(title = "主数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ManuKpiMasterBo bo) {
        return toAjax(manuKpiMasterService.updateByBo(bo));
    }

    /**
     * 删除主数据
     *
     * @param ids 主键串
     */
    @SaCheckPermission("manu:kpiMaster:remove")
    @Log(title = "主数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(manuKpiMasterService.deleteWithValidByIds(List.of(ids), true));
    }

    @SaCheckRole("superadmin")
    @GetMapping("/generateAllByYear/{year}")
    public R<Void> generateAllByYear(@NotNull(message = "年份不能为空") @PathVariable Integer year) {
        return toAjax(manuKpiMasterService.generateAllByYear(year));
    }
}
